package com.qf.flash.mvp.presenter;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.http.HttpManager;
import com.qf.flash.mvp.contract.UpdatePasswordContract;
import com.qf.flash.utils.LogUtil;
import com.qf.flashlib.base.BasePresenter;
import com.qf.flashlib.base.BaseResponse;
import com.qf.flashlib.base.BaseSubscriber;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.util.RxUtils;

import okhttp3.MultipartBody;

public class UpdatePasswordPresenter extends BasePresenter<UpdatePasswordContract.View> implements UpdatePasswordContract.Presenter {


    @Override
    public void updatePassword(String code, String password) {
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .addFormDataPart("userId", uid)
                .addFormDataPart("code", code)
                .addFormDataPart("password", password);

        addSubscription(HttpManager.getInstance().getGsonHttpApi()
//                .UPDATE_PASSWORD_BYID(builder.build())
                .UPDATE_PASSWORD_BYID(uid,code,password)
                .compose(RxUtils.handleBaseResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<BaseResponse<Object>>(mRootView) {
                    @Override
                    public void onSuccess(BaseResponse<Object> objectBaseResponse) {
                        mRootView.updateResult(objectBaseResponse.isOk());
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        LogUtil.d("ApiException:" + e.getMessage());
                    }
                })
        );
    }

    @Override
    public void updatePasswordByPhone(String mobile, String code, String password) {
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                        .UPDATE_PASSWORD_BYPHO(mobile,code,password)
                        .compose(RxUtils.handleBaseResult())
                        .compose(RxUtils.io_main())
                        .subscribeWith(new BaseSubscriber<BaseResponse<Object>>(mRootView) {
                            @Override
                            public void onSuccess(BaseResponse<Object> objectBaseResponse) {
                                mRootView.updateResult(objectBaseResponse.isOk());
                            }

                            @Override
                            public void onError(ApiException e) {
                                super.onError(e);
                                LogUtil.d("ApiException:" + e.getMessage());
                            }
                        })
        );
    }


}
